<?php

namespace Admin\Controller;

/**
 *  广告控制器
 */
class GuanggaoController extends AdminController {

    public function index() {
//        $list = M("Guanggao")->order('sort asc')->select();

        $list = $this->lists(M("Guanggao"), [], 'sort asc');
        
        $this->assign('_list', $list);
        $this->meta_title = '广告位管理';
        $this->display();
    }

    public function edit() {
        $id = I("id", 0, 'intval');

        if (IS_POST) {

            $data['img_url'] = I("img_url");
            $data['jump_url'] = I("jump_url");
            $data['sort'] = I("sort", 99, 'intval');

            $data['update_time'] = date('Y-m-d H:i:s', time());



            if (empty($data['img_url'])) {
                $this->error('图片必须添加!');
            }
            if (empty($data['jump_url'])) {
                $this->error('跳转网址!');
            }

            if ($id == 0) {
                $id = M('Guanggao')->add($data);
            } else {
                $res = M('Guanggao')->where(array('id' => $id))->save($data);
            }


            if ($id) {
                $this->success('操作成功', U('Index'));
            } else {
                $this->error('操作失败');
            }
        } else {
            $data = M('Guanggao')->where(array("id" => $id))->find();

            $this->assign('data', $data);
            $this->meta_title = '添加/编辑广告';
            $this->display();
        }
    }

}
